<?php
/**
 * Description of YCommon_Weixin_Validation
 *
 * @author balcksunshine
 */
require_once dirname(dirname(__FILE__)) . '/tool/YTool_Input.php';
class YCommon_Weixin_Validation {
    
    public $validation;
    protected $signature;
    protected $timestamp;
    protected $nonce;
    protected $echostr;
    
    const TOKEN = 'blacksunshine';
    
    public function __construct() {
        $this->getPara();
        $this->doValidation();
    }
    
    protected function getPara()
    {
        $this->signature = YTool_Input::clean('r', 'signature', 'str');
        $this->timestamp = YTool_Input::clean('r', 'timestamp', 'str');
        $this->nonce = YTool_Input::clean('r', 'nonce', 'str');
        $this->echostr = YTool_Input::clean('r', 'echostr', 'str');
    }
    
    protected function doValidation()
    {
        $sorted = array(self::TOKEN, $this->timestamp, $this->nonce);
	sort($sorted);
	$weixinSign = sha1(implode($sorted));
	if ($weixinSign == $this->signature) {
		$this->validation = array($this->signature, $this->echostr);
	} else {
		$this->validation = true;
	}
    }
}

?>
